<!doctype html>
<html>

<head>
    <title>Socket.IO chat</title>
    <script src="./socket.io.min.js"></script>
    <script src="./vue@2.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font: 13px Helvetica, Arial;
        }

        .form {
            background: #000;
            padding: 3px;
            position: fixed;
            bottom: 0;
            width: 100%;
        }

        .form input {
            border: 0;
            padding: 10px;
            width: 90%;
            margin-right: .5%;
        }

        .form button {
            width: 9%;
            background: rgb(130, 224, 255);
            border: none;
            padding: 10px;
        }

        #messages {
            list-style-type: none;
            padding: 0;
            margin-bottom: 100px;
        }

        #messages li {
            padding: 5px 10px;
        }

        #messages li:nth-child(odd) {
            background: #eee;
        }

        #msg {
            background-color: antiquewhite;
            color: black;
            font-weight: 600;
            font-size: 20px;
            text-align: center;
            width: 100%;
            padding: 10px 0;
        }
    </style>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="item in list" :key="item">
                {{item}}
            </li>
        </ul>
        <div class="form">
            <input autocomplete="off" v-model="message" />
            <button v-on:click="send">Send</button>
        </div>
    </div>
    <script>
        // var socket = io("ws://172.19.8.86:8090");
        const socket = io("http://127.0.0.1:30001");

        // 连接成功
        socket.on("connect", () => {
            console.log(socket.id); // x8WIv7-mJelg7on_ALbx
            socket.on('chat message', (data) => {
                console.log('%c 🍊 chat message: ', 'font-size:20px;background-color: #465975;color:#fff;', data);
            })
        });

        // 断开连接
        socket.on("disconnect", () => {
            console.log(socket.id); // undefined
        });

        // 连接失败
        socket.on("connect_error", (reason) => {
            console.log('%c 🦑 connect_error: ', 'font-size:20px;background-color: #FCA650;color:#fff;', reason);
            setTimeout(() => {
                socket.connect();
            }, 1000);
        });

        var app = new Vue({
            el: '#app',
            data: {
                message: '',
                list: [1, 2, 3]
            },
            methods: {
                send: function () {
                    console.log('%c 🍨 this.message: ', 'font-size:20px;background-color: #2EAFB0;color:#fff;', this.message);
                    socket.emit('chat message', this.message);
                }
            },
        })
    </script>
</body>

</html>